<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function index(){
		$data['title'] = "New Paltz HvZ - Register";
		$this->load->helper('form');
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('register_view');
		$this->load->view('includes/footer_view');
		
	}
	
	public function _newUser(){
		$this->load->library("form_validation");
		$this->load->model('Register_model');
		//$this->membership_model->validate_registration();
		$this->form_validation->set_rules("username","Username","trim|required|min_length[6]|max_length[100]|alpha_numeric|is_unique[users.username]xss_clean");
		$this->form_validation->set_rules("password", "Password","trim|required|min_length[6]|max_length[45]|alpha_numeric|md5|xss_clean");
		$this->form_validation->set_rules("repeat_password", "Repeat Password","trim|required|min_length[6]|max_length[32]|alpha_numeric|matches(password)|xss_clean");
		$this->form_validation->set_rules("first_name", "First Name","trim|required|alpha|xss_clean");
		$this->form_validation->set_rules("last_name", "Last Name","trim|required|alpha|xss_clean");
		$this->form_validation->set_rules("email", "Email","trim|required|validate_email|xss_clean");
		
		if($this->form_validation->run() == FALSE){
			//$data['title'] = "New Paltz HvZ - Register";
			//$this->load->helper('form');
			$this->register();
			//$this->load->view('signin_view.php', $data);
		}
		else{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$email = $this->input->post('email');
			$first_name = $this->input->post('first_name');
			$last_name = $this->input->post('last_name');
			$remember_me = $this->input->post('remember_me');
			$verification_code = _randomString(8);
			$priority = 1;
			$this->User_model->validate_registration($username,$password,$email,$first_name,$last_name,$priority,$verification_code);
			
			
			$killcode = _randomString(8);
			$this->User_model->insert_current_game($username,$killcode);
			//email Sending
			$this->load->library('email');
			$this->email->from("hvz.sunynewpaltz@gmail.com", "New Paltz - HvZ");
			$this->email->to($email);
			$this->email->subject("New Paltz HvZ Registration");
			$this->email->message("Thank you for registring to New Paltz Humans Vs Zombies.".anchor("http://www.newpaltz.com/home/index/","Home Page"));
			$this->email->send();
			
			redirect("http://www.newpaltzhvz.com/profile/signin/");
			//confirmation view
			
		}
		
	}
	
	//Helper functions.
	
	function _randomString($length){
		$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$string ="";
		for($i=0;$i<$length;$i++)
			$string .= $characters[rand(0, strlen($characters) - 1)];
		return $string;
	}
	
	
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */